/**
 * Project: springyounger.lottery.dal
 * 
 * File Created at 2010-3-26
 * $Id$
 * 
 * Copyright 2009 Zjut Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Zjut Edu. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Zjut Edu.
 */
package com.springyounger.lottery.dal.dao.ibatis;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.springyounger.lottery.dal.dao.PhaseDao;
import com.springyounger.lottery.dal.dataobject.LotteryPhase;
import com.springyounger.lottery.dal.dataobject.LotteryPrize;
import com.springyounger.lottery.dal.dataobject.LotteryPrizeQuota;
import com.springyounger.lottery.dal.dataobject.LotteryQuota;
import com.springyounger.lottery.dal.dataobject.LotteryStatistical;
import com.springyounger.lottery.dal.dataobject.LotteryWinner;

/**
 * PhaseDaoImpl
 * 
 * @author springyounger
 */
public class PhaseDaoImpl extends SqlMapClientDaoSupport implements PhaseDao {

    @SuppressWarnings("unchecked")
    public List<LotteryPhase> getPhases() {
        Map params = new HashMap();
        return (List<LotteryPhase>) getSqlMapClientTemplate().queryForList("getPhases", params);
    }

    public LotteryPhase getPhaseById(int phaseId) {
        return (LotteryPhase) getSqlMapClientTemplate().queryForObject("getPhaseById",
                new Integer(phaseId));
    }

    @SuppressWarnings("unchecked")
    public List<LotteryPhase> getPhases(int pageIndex, int pageSize) {
        Map params = new HashMap();
        pageIndex = pageIndex * pageSize;
        if (pageIndex < 0) {
            pageIndex = 0;
        }
        params.put("pageIndex", pageIndex);
        params.put("pageSize", pageSize);
        return (List<LotteryPhase>) getSqlMapClientTemplate().queryForList("getPhases", params);
    }

    public LotteryQuota getLotteryQuota(LotteryQuota quota) {
        return (LotteryQuota) getSqlMapClientTemplate().queryForObject("getLotteryQuota", quota);
    }

    public LotteryQuota insertLotteryQuota(LotteryQuota quota) {
        getSqlMapClientTemplate().insert("insertLotteryQuota", quota);
        return quota;
    }

    public int updateLotteryQuota(LotteryQuota quota) {
        return getSqlMapClientTemplate().update("updateLotteryQuota", quota);
    }

    @SuppressWarnings("unchecked")
    public List<LotteryPrizeQuota> getPrizes(int phaseId, Date grantDate) {
        Map params = new HashMap();
        params.put("phaseId", new Integer(phaseId));
        params.put("grantDate", grantDate);
        return (List<LotteryPrizeQuota>) getSqlMapClientTemplate().queryForList(
                "getPrizesByPhaseId", params);
    }

    public LotteryPrizeQuota getLotteryPrizeQuota(int quotaId) {
        return (LotteryPrizeQuota) getSqlMapClientTemplate().queryForObject("getLotteryPrizeQuota",
                new Integer(quotaId));
    }

    public int updateLotteryPrizeQuota(LotteryPrizeQuota prize) {
        return getSqlMapClientTemplate().update("updateLotteryPrizeQuota", prize);
    }

    public LotteryWinner insertLotteryWinner(LotteryWinner winner) {
        getSqlMapClientTemplate().insert("insertLotteryWinner", winner);
        return winner;
    }

    public int getAllPhaseCount() {
        return (Integer) getSqlMapClientTemplate().queryForObject("getAllPhaseCount");
    }

    @SuppressWarnings("unchecked")
    public List<LotteryWinner> getLatestWinner(int phaseId, int topSize) {
        Map params = new HashMap();
        params.put("phaseId", phaseId);
        params.put("topSize", topSize);
        return (List<LotteryWinner>) getSqlMapClientTemplate().queryForList("getLatestWinner",
                params);
    }

    @SuppressWarnings("unchecked")
    public int getAllWinnerCountById(int userid, int phaseId) {
        Map params = new HashMap();
        params.put("userid", userid);
        params.put("phaseId", phaseId);
        return (Integer) getSqlMapClientTemplate().queryForObject("getAllWinnerById", params);
    }

    @SuppressWarnings("unchecked")
    public List<LotteryWinner> getWinnerById(int userid, int phaseId, int pageIndex, int pageSize) {
        Map params = new HashMap();
        params.put("userid", userid);
        params.put("phaseId", phaseId);
        pageIndex = pageIndex * pageSize;
        if (pageIndex < 0) {
            pageIndex = 0;
        }
        params.put("pageIndex", pageIndex);
        params.put("pageSize", pageSize);
        return (List<LotteryWinner>) getSqlMapClientTemplate()
                .queryForList("getWinnerById", params);
    }

    @SuppressWarnings("unchecked")
    public List<LotteryPrize> getAllPrizes(int pageIndex, int pageSize) {
        Map params = new HashMap();
        pageIndex = pageIndex * pageSize;
        if (pageIndex < 0) {
            pageIndex = 0;
        }
        params.put("pageIndex", pageIndex);
        params.put("pageSize", pageSize);
        return (List<LotteryPrize>) getSqlMapClientTemplate().queryForList("getAllPrizes", params);
    }

    public int getAllPrizesCount() {
        return (Integer) getSqlMapClientTemplate().queryForObject("getAllPrizesCount");
    }

    public LotteryPrize getPrizeById(int prizeId) {
        return (LotteryPrize) getSqlMapClientTemplate().queryForObject("getPrizeById",
                new Integer(prizeId));
    }

    public int deletePrize(int prizeId) {
        return (Integer) getSqlMapClientTemplate().delete("deletePrize", new Integer(prizeId));
    }

    public LotteryPrize insertPrize(LotteryPrize prize) {
        getSqlMapClientTemplate().insert("insertPrize", prize);
        return prize;
    }

    public int updatePrize(LotteryPrize prize) {
        return getSqlMapClientTemplate().update("updatePrize", prize);
    }

    public int isExistPrize(LotteryPrize prize) {
        return (Integer) getSqlMapClientTemplate().queryForObject("getPrizeByPhaseLevel", prize);
    }

    public int deletePhaseById(int phaseId) {
        return (Integer) getSqlMapClientTemplate().delete("deletePhaseById", phaseId);
    }

    public LotteryPhase insertPhase(LotteryPhase phase) {
        getSqlMapClientTemplate().insert("insertPhase", phase);
        return phase;
    }

    public int updatePhase(LotteryPhase phase) {
        return getSqlMapClientTemplate().update("updatePhase", phase);
    }

    public int deletePrizeByPhaseId(int phaseId) {
        return getSqlMapClientTemplate().delete("deletePrizeByPhaseId", phaseId);
    }

    public int isExistPhase(String phaseName) {
        return (Integer) getSqlMapClientTemplate().queryForObject("selectPhaseByName", phaseName);
    }

    @SuppressWarnings("unchecked")
    public List<LotteryPrizeQuota> getAllPrizesByPhaseId(int phaseId) {
        return (List<LotteryPrizeQuota>) getSqlMapClientTemplate().queryForList(
                "getAllPrizesByPhaseId", phaseId);
    }

    public LotteryPrizeQuota getTopPrizeQuota(int prizeId) {
        return (LotteryPrizeQuota) getSqlMapClientTemplate().queryForObject("getTopPrizeQuota",
                prizeId);
    }

    public void batchInsertPrizeQuota(final List<LotteryPrizeQuota> quotaList) {
        getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                //批量插入
                executor.startBatch();
                for (LotteryPrizeQuota quota : quotaList) {
                    executor.insert("insertPrizeQuota", quota);
                }
                executor.executeBatch();
                return null;
            }
        });
    }

    public void batchUpdatePrizeQuota(final List<LotteryPrizeQuota> quotaList) {
        getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                //批量更新
                executor.startBatch();
                for (LotteryPrizeQuota quota : quotaList) {
                    executor.insert("updatePrizeQuota", quota);
                }
                executor.executeBatch();
                return null;
            }
        });
    }

    @SuppressWarnings("unchecked")
    public List<LotteryStatistical> getStat(int phaseId) {
        return (List<LotteryStatistical>) getSqlMapClientTemplate()
                .queryForList("getStat", phaseId);
    }

    public void batchInsertStat(final List<LotteryStatistical> statList) {
        getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                executor.startBatch();
                for (LotteryStatistical stat : statList) {
                    executor.insert("insertStat", stat);
                }
                executor.executeBatch();
                return null;
            }
        });
    }

    @SuppressWarnings("unchecked")
    public LotteryStatistical getStatValue(int phaseId, String prizeLevel, Date lotteryDate) {
        Map params = new HashMap();
        params.put("phaseId", phaseId);
        params.put("prizeLevel", prizeLevel);
        params.put("lotteryDate", lotteryDate);
        return (LotteryStatistical) getSqlMapClientTemplate()
                .queryForObject("getStatValue", params);
    }

    @SuppressWarnings("unchecked")
    public int getStatByDate(int phaseId, Date winDate) {
        Map params = new HashMap();
        params.put("phaseId", phaseId);
        params.put("winDate", winDate);
        return (Integer) getSqlMapClientTemplate().queryForObject("getStatByDate", params);
    }
}
